package com.stedu.controller;
//备份


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.stedu.bean.PeiJian;
import com.stedu.bean.RespBean;
import com.stedu.exception.SteduException;
import com.stedu.mapper.PeiJianMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.sql.SQLException;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/admin/peiJian")
public class PeiJianController {

    @Autowired
    private PeiJianMapper peiJianMapper;
    @GetMapping
    protected RespBean selectByPage(@RequestParam(value = "pageNum") Integer pageNum, @RequestParam(value = "partName",defaultValue = "") String partName, @RequestParam(value = "partCode",defaultValue = "") String partCode) throws SQLException {
        if (pageNum==null){
            pageNum = 1;
        }
        PageHelper.startPage(pageNum, 5);
        List<PeiJian> oldmanList = peiJianMapper.selectByPnameAndOphone(partName,partCode);
        PageInfo<PeiJian> pageInfo = new PageInfo<>(oldmanList);
        return RespBean.ok("查询成功",pageInfo);
    }


    @PostMapping
    public RespBean insert(@Validated @RequestBody PeiJian peiJian) throws SteduException {
        //添加
        if (peiJianMapper.insert(peiJian)) {
             return RespBean.ok("添加成功~~~");
        } else {
            return RespBean.error("添加失败，请重新添加");
        }
    }

    @DeleteMapping("/{partId}")
    public RespBean delete(@PathVariable("partId") Integer partId) throws SteduException {
        if (peiJianMapper.delete(partId)) {
            return RespBean.ok("删除成功~~~");
        } else {
            return RespBean.error("删除失败，请重新删除");
        }
    }

    @PutMapping
    public RespBean update(@Validated @RequestBody PeiJian peiJian) throws SteduException {
        if (peiJianMapper.update(peiJian)) {
            return RespBean.ok("修改成功~~~");
        } else {
            return RespBean.error("修改失败，请重新修改");
        }
    }


    @GetMapping("/{partId}")
    public RespBean selectByOid(@PathVariable("partId") Integer partId) {
        PeiJian d = peiJianMapper.selectByPid(partId);
        return RespBean.ok("", d);
    }
    @GetMapping("/all/{partId}")
    @CrossOrigin(origins = "http://localhost:5173")
    public RespBean getAllParts(@PathVariable("partId") Integer partId) {
        List<PeiJian> dataList = peiJianMapper.selectAllParts1(partId);
        return RespBean.ok("", dataList);
    }
}
